/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game;

import framework.GameObject;
import framework.Key;
import framework.Screen;
import framework.Util;
import framework.gameAPI.Sprite;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

/**
 *
 * @author edilson
 */
public class Nave extends GameObject {

    private Sprite sprite;
    private static final int[] ANIM_MOVE = {0, 1};
    private static final int[] ANIM_DEAD = {2, 3, 4, 5, 6, 7, 8, 9};
    private int state;
    public static final int STATE_MOVING = 0;
    public static final int STATE_DYING = 1;
    public static final int STATE_DEAD = 2;
    private int tiro;
    private int[] municao = {100, 0, 0, 0, 0, 0};
    private int tipoTiro;

    public Nave() {

        power = 100;

        this.image = Util.loadImage(Global.IMG_NAVE1);

        sprite = new Sprite(image, 50, 50);
        sprite.setAnimationDelay(10);
        sprite.setFrameSequence(ANIM_MOVE);
        state = STATE_MOVING;



        this.x = Screen.getWidth() / 2;
        this.y = Screen.getHeight() / 2;
        this.width = image.getWidth(null);
        this.height = image.getHeight(null);
        this.width = 50;
        this.height = 50;
        tiro = 1;
        this.tipoTiro = Tiro.TYPE_1;

    }

    @Override
    public void update() {

        switch (state) {
            case STATE_MOVING:
                if (power <= 0) {
                    state = STATE_DYING;
                    sprite.setFrameSequence(ANIM_DEAD);
                }
                if (Key.isDown(KeyEvent.VK_LEFT)) {
                    speedX = -5;
                } else if (Key.isDown(KeyEvent.VK_RIGHT)) {
                    speedX = 5;
                } else {
                    speedX = 0;
                }

                if (Key.isDown(KeyEvent.VK_UP)) {
                    speedY = -5;
                } else if (Key.isDown(KeyEvent.VK_DOWN)) {
                    speedY = 5;
                } else {
                    speedY = 0;
                }



                if (x <= 0) {
                    x = 0;
                }

                if (x >= Screen.getWidth() - 50) {
                    x = Screen.getWidth() - 50;
                }


                if (y <= 0) {
                    y = 0;
                }

                if (y >= Screen.getHeight() - 50) {
                    y = Screen.getHeight() - 50;
                }

                // movendo a nave
                x += speedX;
                y += speedY;

                break;
            case STATE_DYING:
                //sprite.setAnimationDelay(3);
                if (sprite.animationEnded()) {
                    state = STATE_DEAD;
                }
                break;
            case STATE_DEAD:
                setVisible(false);
                break;
        }


    }

    @Override
    public void paint(Graphics g) {
        if (visible) {
            //g.drawImage(image, x, y, null);
            sprite.setX(x);
            sprite.setY(y);
            sprite.paint(g);
        }
    }

    public int getState() {
        return state;
    }

    public int getTiro() {
        this.tiro = (getMunicao(tipoTiro)/100)+1;
        if(this.tiro>=6)
            this.tiro = 6;
        return tiro;
    }

    public void setTiro(int tiro) {
        this.tiro = tiro;
    }

    public void addMunicao(Integer tipoTiro, int quantidade) {
        if (tipoTiro >= 0 && tipoTiro <= 6) {
            municao[tipoTiro - 1] += quantidade;
        }
    }
    public int getMunicao(Integer tipoTiro){
        if (tipoTiro >= 0 && tipoTiro <= 6) {
            return municao[tipoTiro - 1];
        }
        return 0;
    }

    public int getTipoTiro() {
        return tipoTiro;
    }

    public void setTipoTiro(int tipoTiro) {
        this.tipoTiro = tipoTiro;
    }



}
